﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Vortex.Drawing {

	/// <summary>
	/// Base class for any kind of device resources
	/// </summary>
	public abstract class Resource : DeviceListener, IDisposable {
		/// <summary>
		/// Initializes this resource.
		/// </summary>
		protected Resource() {
			Device = DrawingDevice.Current;
			Device.RegisterResource(this);
			base.StartListeningDevice(Device);
		}

		/// <summary>
		/// Gets drawing device used for creation of this resource.
		/// </summary>
		/// <value>The device.</value>
		public DrawingDevice Device { get; private set; }

		/// <summary>
		/// Gets boolean value indicating whether this <see cref="Resource"/> is disposed.
		/// </summary>
		/// <value><c>true</c> if disposed; otherwise, <c>false</c>.</value>
		public bool Disposed { get; private set; }

		#region Resource Management Methods

		/// <summary>
		/// Frees the internal data of resource.
		/// </summary>
		protected virtual void FreeData() { }

		#endregion

		#region IDisposable Members

		/// <summary>
		/// Performs freeing unmanaged resources related to device.
		/// </summary>
		public void Dispose() {
			if (!Disposed) {
				Device.UnregisterResource(this);
				base.StopListeningDevice(Device);

				FreeData();
				Disposed = true;
			}
		}

		#endregion
	}
}
